package com.example.source_share.service.impl.user.account;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.source_share.mapper.UserMapper;
import com.example.source_share.pojo.User;
import com.example.source_share.service.user.account.RegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class RegisterServiceImpl implements RegisterService {
    @Autowired
    UserMapper userMapper;
    @Override
    public Map<String, String> register(String username, String password, String confirmPassword) {
        Map<String,String> map=new HashMap<>();
        if(username==null || username.equals("")){
            map.put("message","用户名为空");
            return map;
        }
        if(password==null || password.equals("")){
            map.put("message","密码为空");
            return map;
        }
        if(!password.equals(confirmPassword)){
            map.put("message","两次密码不一致");
            return map;
        }
        if(password.length()>30){
            map.put("message","密码不能超过30");
            return map;
        }
        if(password.length()<6){
            map.put("message","密码不能少于6位");
            return map;
        }
        if(username.length()>20){
            map.put("message","用户名不能超过20");
            return map;
        }
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        User exist=userMapper.selectOne(queryWrapper);
        if(exist!=null){
            map.put("message","用户名已存在");
            return map;
        }
        PasswordEncoder passwordEncoder=new BCryptPasswordEncoder();
        String encodedPassword=passwordEncoder.encode(password);
        User user=new User(username,encodedPassword);
        userMapper.insert(user);
        map.put("message","Success!");
        return map;
    }
}
